package io.seata.samples.zk.common.aop;

import io.seata.samples.zk.common.enums.RspStatusEnum;
import io.seata.samples.zk.common.exception.DefaultException;
import io.seata.samples.zk.common.response.ObjectResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 全局异常处理
 *
 * @author Administrator
 */
@Slf4j
@ControllerAdvice(basePackages = "io.seata.samples.zk")
public class GlobalExceptionHandler {
    @ResponseBody
    @ExceptionHandler(DefaultException.class)
    public ObjectResponse defaultException(DefaultException e) {
        log.error("系统抛出 DefaultException 异常", e);
        ObjectResponse objectResponse = new ObjectResponse<>();
        objectResponse.setStatus(RspStatusEnum.FAIL.getCode());
        objectResponse.setMessage(RspStatusEnum.FAIL.getMessage());
        return objectResponse;
    }

    @ResponseBody
    @ExceptionHandler(Exception.class)
    public ObjectResponse exceptionHandler(Exception e) {
        log.error("系统抛出 Exception 异常", e);
        ObjectResponse objectResponse = new ObjectResponse<>();
        objectResponse.setStatus(RspStatusEnum.FAIL.getCode());
        objectResponse.setMessage(RspStatusEnum.FAIL.getMessage());
        return objectResponse;
    }
}
